package com.javaweb.listener;

import java.util.Map;
import java.util.Set;

import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;

import com.javaweb.base.BaseSystemMemory;

public class ApplicationEnvironmentListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
	
	private final String CURRENT_ACTIVE_PROPERTY = "org.springframework.boot.env.OriginTrackedMapPropertySource";

	@Override
	public void onApplicationEvent(ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent) {
		//System.getProperties().list(System.out);
		ConfigurableEnvironment configurableEnvironment = applicationEnvironmentPreparedEvent.getEnvironment();
		MutablePropertySources mutablePropertySources = configurableEnvironment.getPropertySources();
		mutablePropertySources.forEach(e->{
			//System.out.println(e.getClass().getName());
			if(CURRENT_ACTIVE_PROPERTY.equals(e.getClass().getName())){//当前激活的配置文件信息
				//Map<String,Object> map = (Map<String,Object>)e.getSource();
				if(e.getSource() instanceof Map<?,?> map) {
					Set<?> set = map.keySet();
					for(Object key:set){
						BaseSystemMemory.currentActivePropertyMap.put(key.toString(),map.get(key));
					}
				}
			}
		});
	}

}
